package strcy.cn.market.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import strcy.cn.market.mapper.OrderDetailsMapper;
import strcy.cn.market.mapper.OrdersMapper;
import strcy.cn.market.pojo.OrderDetails;
import strcy.cn.market.pojo.Orders;
import strcy.cn.market.pojo.ProductCategories;
import strcy.cn.market.pojo.VO.OrderRequest;
import strcy.cn.market.service.OrdersService;
import strcy.cn.market.utils.PageBean;
import strcy.cn.market.utils.ThreadLocalUtil;


import java.util.List;
import java.util.Map;


@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Override
    public void add(Orders orders) {
        Map<String,Object> map =  ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        orders.setUserId(userId);
        orders.setOrderStatus(1);
        ordersMapper.add(orders);
    }

    @Override
    public void insertOrderDetail(OrderDetails orderDetails) {
        orderDetailsMapper.add(orderDetails);
    }

    @Override
    public void updatePayTime(String orderId, Integer orderStatus) {
        ordersMapper.update(orderId,orderStatus);
    }

    @Override
    public List<Orders> list() {
        //调用mapper
        Map<String,Object> map =  ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Orders> as =ordersMapper.list(userId);
        return as;
    }

    @Override
    public PageBean<Orders> lists(int pageNum, int pageSize) {
        //创建pagebean对象
        PageBean<Orders> pb = new PageBean<>();
        //开启分页查询PageHelper
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper
        List<Orders> as =ordersMapper.lists();
        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录数和当前页数据
        Page<Orders> p = (Page<Orders>) as;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}
